String.prototype.trim = function () {
  return this.replace(/(^s+)|(s+$)/g, "");
};

IniConfig = function (iniFileName) {
  this.iniFileName = iniFileName;
  this._iniSecDictionary = new Array();
  this.fso = new ActiveXObject("Scripting.FileSystemObject");
};

IniConfig.prototype._checkFile = function () {
  if (!this.fso.FileExists(this.iniFileName)) {
    this.fso.CreateTextFile(this.iniFileName, true, true);
  }
};

IniConfig.prototype.load = function () {
  this._checkFile();
  var currSecName = null;
  //var fs = this.fso.OpenTextFile(this.iniFileName, 1, false, -1);
   var fs = this.fso.OpenTextFile(this.iniFileName, 1, 0);

  while (!fs.AtEndOfStream) {
    var strLine = fs.ReadLine().trim();
    if (strLine.length > 0) {
      var firchCh = strLine.substr(0, 1);
      if (firchCh != "#") {
        if (firchCh == "[") {
          var secName = strLine.substr(1, strLine.length - 2);
          currSecName = secName;
          this._iniSecDictionary[secName] = new Array();
        } else {
          var idx = strLine.indexOf("=");
          var strKey = strLine.substring(0, idx);
          var strVal = strLine.substr(idx + 1);
          if (currSecName == null) {
            throw "Ini文件格式不正确!";
          }
          this._iniSecDictionary[currSecName][strKey] = strVal;
        }
      }
    }
  }
  fs.Close();
  fs = null;
};

IniConfig.prototype.save = function () {
  this._checkFile();
  var dic = this._iniSecDictionary;
  var currSecName = null;
  var fs = this.fso.OpenTextFile(this.iniFileName, 2, true, -1);
  for (var sec in dic) {
    fs.WriteLine("[" + sec + "]");
    for (var key in dic[sec]) {
      fs.WriteLine(key + "=" + dic[sec][key]);
    }
  }
  fs.Close();
  fs = null;
};

IniConfig.prototype.get = function (secName, keyName) {
  var dic = this._iniSecDictionary;
  try {
    return dic[secName][keyName];
  } catch (e) {
    return "";
  }
};

IniConfig.prototype.set = function (secName, keyName, val) {
  var dic = this._iniSecDictionary;
  try {
    if (dic[secName] == null) {
      dic[secName] = new Array();
    }
    dic[secName][keyName] = val;
  } catch (e) {
    alert(e.message);
  }
};
